<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>梦幻贪吃蛇</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    body {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background: linear-gradient(135deg, #f5f7fa 0%, #ffeef8 100%);
      font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
      color: #5a5a5a;
      flex-direction: column;
      text-align: center;
      overflow: hidden;
      position: relative;
    }

    /* 添加浮动气泡背景 */
    .bubble {
      position: absolute;
      border-radius: 50%;
      background: rgba(255, 182, 193, 0.2);
      animation: float 15s infinite linear;
      z-index: -1;
    }

    @keyframes float {
      0% { transform: translateY(0) rotate(0deg); }
      100% { transform: translateY(-1000px) rotate(720deg); }
    }

    .game-container {
      position: relative;
      width: 440px;
      height: 500px;
      display: flex;
      flex-direction: column;
      align-items: center;
    }

    canvas {
      background-color: #fff9fb;
      border: 3px solid #ffd6e7;
      box-shadow: 0 10px 30px rgba(255, 105, 180, 0.2);
      border-radius: 15px;
      margin-bottom: 20px;
      transition: all 0.3s;
    }

    canvas:hover {
      box-shadow: 0 15px 35px rgba(255, 105, 180, 0.3);
    }

    .game-over {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      font-size: 32px;
      font-weight: bold;
      color: #ff4757;
      font-family: 'Comic Sans MS', cursive, sans-serif;
      text-shadow: 2px 2px 8px rgba(255, 71, 87, 0.3);
      background: rgba(255, 255, 255, 0.9);
      padding: 20px 40px;
      border-radius: 50px;
      box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
      display: none;
      z-index: 10;
      animation: pulse 1.5s infinite;
    }

    @keyframes pulse {
      0% { transform: translate(-50%, -50%) scale(1); }
      50% { transform: translate(-50%, -50%) scale(1.05); }
      100% { transform: translate(-50%, -50%) scale(1); }
    }

    .score {
      font-size: 24px;
      font-weight: bold;
      color: #ff6b81;
      margin-bottom: 15px;
      font-family: 'Comic Sans MS', cursive, sans-serif;
      text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.1);
      background: white;
      padding: 10px 25px;
      border-radius: 30px;
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
    }

    .controls {
      display: flex;
      gap: 10px;
      margin-top: 10px;
    }

    .control-btn {
      width: 60px;
      height: 60px;
      background: linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%);
      border: none;
      border-radius: 50%;
      color: white;
      font-size: 24px;
      cursor: pointer;
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
      transition: all 0.3s;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .control-btn:hover {
      transform: translateY(-3px);
      box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
    }

    .control-btn:active {
      transform: translateY(1px);
      box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
    }

    .start-btn {
      padding: 15px 40px;
      font-size: 20px;
      background: linear-gradient(45deg, #ff9a9e, #fad0c4);
      color: white;
      border: none;
      border-radius: 50px;
      cursor: pointer;
      box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
      transition: all 0.3s;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      font-family: 'Comic Sans MS', cursive;
      z-index: 10;
      font-weight: bold;
      letter-spacing: 1px;
    }

    .start-btn:hover {
      transform: translate(-50%, -50%) scale(1.05);
      box-shadow: 0 15px 25px rgba(0, 0, 0, 0.15);
      background: linear-gradient(45deg, #fad0c4, #ff9a9e);
    }

    .start-btn:active {
      transform: translate(-50%, -50%) scale(0.98);
    }

    .back-button {
      position: absolute;
      left: 30px;
      top: 30px;
      padding: 12px 25px;
      background: linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%);
      color: white;
      text-decoration: none;
      border-radius: 50px;
      transition: all 0.3s;
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
      border: none;
      font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
      font-weight: bold;
      display: flex;
      align-items: center;
      gap: 5px;
    }

    .back-button:hover {
      transform: translateY(-3px);
      box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
    }

    .back-button:active {
      transform: translateY(1px);
    }

    .game-title {
      font-size: 36px;
      font-weight: bold;
      color: #ff6b81;
      margin-bottom: 20px;
      font-family: 'Comic Sans MS', cursive;
      text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
    }

    .mobile-controls {
      display: none;
    }

    @media (max-width: 500px) {
      .game-container {
        width: 100%;
        padding: 0 20px;
      }

      canvas {
        width: 100%;
        height: auto;
        aspect-ratio: 1/1;
      }

      .mobile-controls {
        display: flex;
        flex-direction: column;
        gap: 10px;
        margin-top: 20px;
      }

      .mobile-row {
        display: flex;
        justify-content: center;
        gap: 10px;
      }

      .back-button {
        left: 15px;
        top: 15px;
        padding: 8px 15px;
        font-size: 14px;
      }
    }
  </style>
</head>
<body>
  <a href="/game/" class="back-button">← 返回</a>

  <div class="game-container">
    <h1 class="game-title">梦幻贪吃蛇</h1>
    <div class="score" id="score">分数: 0</div>
    <button class="start-btn" id="startBtn">开始游戏</button>
    <div class="game-over" id="gameOver">游戏结束!</div>
    <canvas id="gameCanvas" width="400" height="400"></canvas>

    <div class="mobile-controls">
      <div class="mobile-row">
        <button class="control-btn" id="upBtn">↑</button>
      </div>
      <div class="mobile-row">
        <button class="control-btn" id="leftBtn">←</button>
        <button class="control-btn" id="downBtn">↓</button>
        <button class="control-btn" id="rightBtn">→</button>
      </div>
    </div>
  </div>

  <script>
    // 创建背景气泡
    function createBubbles() {
      const bubbleCount = 15;
      for (let i = 0; i < bubbleCount; i++) {
        const bubble = document.createElement('div');
        bubble.classList.add('bubble');

        const size = Math.random() * 100 + 50;
        const posX = Math.random() * window.innerWidth;
        const delay = Math.random() * 15;
        const duration = Math.random() * 20 + 10;

        bubble.style.width = `${size}px`;
        bubble.style.height = `${size}px`;
        bubble.style.left = `${posX}px`;
        bubble.style.bottom = `-${size}px`;
        bubble.style.animationDelay = `${delay}s`;
        bubble.style.animationDuration = `${duration}s`;

        document.body.appendChild(bubble);
      }
    }

    createBubbles();

    let isPlaying = false;
    // 获取画布和上下文
    const canvas = document.getElementById("gameCanvas");
    const ctx = canvas.getContext("2d");

    // 游戏常量
    const boxSize = 20; // 每一格的大小
    const canvasSize = 400; // 画布大小
    const rows = canvasSize / boxSize;
    const cols = canvasSize / boxSize;

    // 游戏状态
    let snake = [{ x: 8, y: 8 }]; // 初始蛇的位置
    let direction = "RIGHT"; // 初始方向
    let food = generateFood(); // 初始食物
    let gameInterval; // 游戏的计时器
    let score = 0;
    let highScore = localStorage.getItem('snakeHighScore') || 0;

    // 生成随机食物
    function generateFood() {
      let newFood;
      do {
        newFood = {
          x: Math.floor(Math.random() * cols),
          y: Math.floor(Math.random() * rows),
          color: getRandomColor()
        };
      } while (snake.some(part => part.x === newFood.x && part.y === newFood.y));

      return newFood;
    }

    // 生成随机颜色
    function getRandomColor() {
      const colors = ['#FF9AA2', '#FFB7B2', '#FFDAC1', '#E2F0CB', '#B5EAD7', '#C7CEEA'];
      return colors[Math.floor(Math.random() * colors.length)];
    }

    // 监听按键
    document.addEventListener("keydown", changeDirection);
    document.getElementById("startBtn").addEventListener("click", restartGame);

    // 移动端控制
    document.getElementById("upBtn").addEventListener("click", () => changeDirection({key: "ArrowUp"}));
    document.getElementById("downBtn").addEventListener("click", () => changeDirection({key: "ArrowDown"}));
    document.getElementById("leftBtn").addEventListener("click", () => changeDirection({key: "ArrowLeft"}));
    document.getElementById("rightBtn").addEventListener("click", () => changeDirection({key: "ArrowRight"}));

    function changeDirection(event) {
      if (!isPlaying) return; // 游戏未开始时禁止操作
      const key = event.key;
      if (key === "ArrowUp" && direction !== "DOWN") direction = "UP";
      if (key === "ArrowDown" && direction !== "UP") direction = "DOWN";
      if (key === "ArrowLeft" && direction !== "RIGHT") direction = "LEFT";
      if (key === "ArrowRight" && direction !== "LEFT") direction = "RIGHT";
      if (key === "r" || key === "R") restartGame(); // 重新开始游戏
    }

    // 绘制游戏
    function drawGame() {
      // 清空画布
      ctx.clearRect(0, 0, canvasSize, canvasSize);

      // 绘制网格背景
      drawGrid();

      // 绘制蛇
      snake.forEach((part, index) => {
        const gradient = ctx.createLinearGradient(
          part.x * boxSize,
          part.y * boxSize,
          part.x * boxSize + boxSize,
          part.y * boxSize + boxSize
        );

        if (index === 0) {
          // 蛇头
          gradient.addColorStop(0, '#FF9AA2');
          gradient.addColorStop(1, '#FFB7B2');
        } else {
          // 蛇身
          gradient.addColorStop(0, '#B5EAD7');
          gradient.addColorStop(1, '#C7CEEA');
        }

        ctx.fillStyle = gradient;
        ctx.beginPath();
        ctx.roundRect(part.x * boxSize, part.y * boxSize, boxSize, boxSize, 5);
        ctx.fill();

        // 蛇眼
        if (index === 0) {
          ctx.fillStyle = 'white';
          ctx.beginPath();
          ctx.arc(
            part.x * boxSize + boxSize/3,
            part.y * boxSize + boxSize/3,
            boxSize/8,
            0,
            Math.PI * 2
          );
          ctx.arc(
            part.x * boxSize + boxSize*2/3,
            part.y * boxSize + boxSize/3,
            boxSize/8,
            0,
            Math.PI * 2
          );
          ctx.fill();

          ctx.fillStyle = 'black';
          ctx.beginPath();
          ctx.arc(
            part.x * boxSize + boxSize/3,
            part.y * boxSize + boxSize/3,
            boxSize/12,
            0,
            Math.PI * 2
          );
          ctx.arc(
            part.x * boxSize + boxSize*2/3,
            part.y * boxSize + boxSize/3,
            boxSize/12,
            0,
            Math.PI * 2
          );
          ctx.fill();
        }
      });

      // 绘制食物
      ctx.fillStyle = food.color;
      ctx.beginPath();
      ctx.arc(
        food.x * boxSize + boxSize/2,
        food.y * boxSize + boxSize/2,
        boxSize/2,
        0,
        Math.PI * 2
      );
      ctx.fill();

      // 食物高光
      ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
      ctx.beginPath();
      ctx.arc(
        food.x * boxSize + boxSize/3,
        food.y * boxSize + boxSize/3,
        boxSize/5,
        0,
        Math.PI * 2
      );
      ctx.fill();

      // 更新蛇
      updateSnake();

      // 绘制得分
      document.getElementById("score").innerText = `分数: ${score} | 最高分: ${highScore}`;
    }

    // 绘制网格背景
    function drawGrid() {
      ctx.strokeStyle = 'rgba(255, 182, 193, 0.1)';
      ctx.lineWidth = 1;

      // 垂直线
      for (let i = 0; i <= cols; i++) {
        ctx.beginPath();
        ctx.moveTo(i * boxSize, 0);
        ctx.lineTo(i * boxSize, canvasSize);
        ctx.stroke();
      }

      // 水平线
      for (let j = 0; j <= rows; j++) {
        ctx.beginPath();
        ctx.moveTo(0, j * boxSize);
        ctx.lineTo(canvasSize, j * boxSize);
        ctx.stroke();
      }
    }

    // 更新蛇的状态
    function updateSnake() {
      const head = { ...snake[0] };

      // 根据方向移动蛇
      if (direction === "UP") head.y -= 1;
      if (direction === "DOWN") head.y += 1;
      if (direction === "LEFT") head.x -= 1;
      if (direction === "RIGHT") head.x += 1;

      // 检查是否撞墙或撞到自己
      if (
        head.x < 0 ||
        head.y < 0 ||
        head.x >= cols ||
        head.y >= rows ||
        snake.some((part) => part.x === head.x && part.y === head.y)
      ) {
        gameOver();
        return;
      }

      // 添加新的蛇头
      snake.unshift(head);

      // 检查是否吃到食物
      if (head.x === food.x && head.y === food.y) {
        score += 10; // 加分

        // 更新最高分
        if (score > highScore) {
          highScore = score;
          localStorage.setItem('snakeHighScore', highScore);
        }

        food = generateFood(); // 生成新食物

        // 加速游戏
        if (score % 50 === 0 && gameInterval > 100) {
          clearInterval(gameInterval);
          gameInterval = setInterval(drawGame, gameInterval - 20);
        }
      } else {
        snake.pop(); // 移除尾部
      }
    }

    // 游戏结束
    function gameOver() {
      isPlaying = false;
      clearInterval(gameInterval);
      document.getElementById("gameOver").style.display = "block";
      document.getElementById("startBtn").style.display = "block";

      // 显示游戏结束动画
      const gameOverText = document.getElementById("gameOver");
      gameOverText.textContent = `游戏结束! 得分: ${score}`;

      // 震动效果
      canvas.style.transform = 'translateX(-5px)';
      setTimeout(() => {
        canvas.style.transform = 'translateX(5px)';
        setTimeout(() => {
          canvas.style.transform = 'translateX(0)';
        }, 50);
      }, 50);
    }

    // 重新开始游戏
    function restartGame() {
      isPlaying = true;
      document.getElementById("startBtn").style.display = "none";
      document.getElementById("gameOver").style.display = "none";
      snake = [{ x: 8, y: 8 }];
      direction = "RIGHT";
      food = generateFood();
      score = 0;
      clearInterval(gameInterval);
      gameInterval = setInterval(drawGame, 200);

      // 重置画布样式
      canvas.style.transform = 'scale(1)';
    }
  </script>
</body>
</html>